<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <div class="header-left">
        <div class="logo-icon"></div>
        <h1>智慧大棚监控系统</h1>
      </div>
      <div class="header-right">
        <div class="current-time">{{ currentTime }}</div>
        <el-button 
          class="logout-btn" 
          type="danger" 
          size="small"
          @click="handleLogout"
        >
          <el-icon><SwitchButton /></el-icon>
          退出登录
        </el-button>
      </div>
    </div>
    
    <div class="dashboard-content">
      <!-- 左侧面板：大棚信息和监测数据 -->
      <div class="left-panel">
        <greenhouse-info :greenhouse-id="currentGreenhouseId" />
      </div>
      
      <!-- 中央面板：视频监控 -->
      <div class="center-panel">
        <div class="video-section">
          <video-preview :greenhouse-id="currentGreenhouseId" />
        </div>
      </div>
      
      <!-- 右侧面板：设备状态 -->
      <div class="right-panel">
        <div class="section-title">
          <el-icon><Connection /></el-icon>
          设备状态
        </div>
        <div class="device-list">
          <el-table
            :data="deviceList"
            style="width: 100%"
            :header-cell-style="{ background: 'transparent', color: '#fff' }"
            :cell-style="{ background: 'transparent', color: '#fff' }"
          >
            <el-table-column prop="name" label="设备名称" />
            <el-table-column prop="status" label="状态">
              <template #default="scope">
                <el-tag :type="scope.row.status === '在线' ? 'success' : 'danger'">
                  {{ scope.row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 天气显示 -->
        <div class="weather-display">
          <div class="weather-title">实时天气预报</div>
          <div class="weather-location">云南大学呈贡校区软件学院蔬菜基地</div>
          <div class="weather-time">昆明 11:28发布</div>
          <div class="weather-gauge" ref="weatherGauge"></div>
          <div class="weather-info">
            <div class="weather-temp-text">多云 7°C~17°C</div>
            <div class="weather-current-temp">18°C</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { Connection, SwitchButton } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import GreenhouseInfo from '@/components/dashboard/GreenhouseInfo.vue'
import VideoPreview from '@/components/dashboard/VideoPreview.vue'
import * as echarts from 'echarts'

const router = useRouter()

const currentGreenhouseId = ref('1')
const deviceList = ref([
  { name: '土壤温度传感器', status: '在线' },
  { name: '土壤pH传感器', status: '在线' },
  { name: '土壤养分传感器', status: '在线' },
  { name: '光照传感器', status: '在线' },
  { name: '摄像头设备', status: '在线' }
])

const switchGreenhouse = () => {
  currentGreenhouseId.value = currentGreenhouseId.value === '1' ? '2' : '1'
}

const currentTime = ref('')
let timeTimer = null

const updateTime = () => {
  currentTime.value = new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  }).replace(/\//g, '-')
}

// 添加天气图表实例
const weatherGauge = ref(null)
const weatherGaugeInstance = ref(null)

// 初始化天气仪表盘
const initWeatherGauge = () => {
  if (!weatherGauge.value) return null
  
  const chart = echarts.init(weatherGauge.value)
  const option = {
    backgroundColor: 'transparent',
    tooltip: {
      show: true,
      trigger: 'item',
      formatter: function(params) {
        if (params.name === '当前温度') {
          return `当前温度: ${params.value}°C`
        } else {
          return `温差: ${params.value}°C`
        }
      },
      textStyle: {
        color: '#fff',
        fontSize: 12
      },
      backgroundColor: 'rgba(0, 21, 41, 0.9)',
      borderColor: 'rgba(0, 255, 255, 0.2)',
      borderWidth: 1,
      padding: [5, 10]
    },
    legend: {
      show: true,
      bottom: -5,
      itemGap: 15,
      textStyle: {
        color: 'rgba(0, 255, 255, 0.8)',
        fontSize: 11,
        padding: [0, 0, 0, 4]
      },
      itemStyle: {
        borderWidth: 0
      },
      data: ['当前温度', '温差']
    },
    series: [{
      type: 'pie',
      radius: ['45%', '65%'],
      center: ['50%', '45%'],
      avoidLabelOverlap: true,
      itemStyle: {
        borderColor: '#001529',
        borderWidth: 2
      },
      label: {
        show: false
      },
      labelLine: {
        show: false
      },
      data: [
        { 
          value: 18, 
          name: '当前温度',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#37a2da' },
              { offset: 1, color: '#67e0e3' }
            ])
          }
        },
        { 
          value: 12, 
          name: '温差',
          itemStyle: {
            color: 'rgba(0, 255, 255, 0.1)'
          }
        }
      ]
    }]
  }
  
  chart.setOption(option)
  return chart
}

// 添加退出登录方法
const handleLogout = () => {
  // 清除token
  localStorage.removeItem('token')
  // 跳转到登录页
  router.push('/login')
}

onMounted(() => {
  updateTime()
  timeTimer = setInterval(updateTime, 1000)
  
  nextTick(() => {
    weatherGaugeInstance.value = initWeatherGauge()
  })
})

onUnmounted(() => {
  if (timeTimer) {
    clearInterval(timeTimer)
  }
  if (weatherGaugeInstance.value) {
    weatherGaugeInstance.value.dispose()
  }
})
</script>

<style scoped>
.dashboard {
  width: 100vw;
  height: 100vh;
  background: #001529;
  color: #fff;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.dashboard-header {
  height: 60px;
  padding: 0 30px;
  background: rgba(0, 21, 41, 0.8);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(0, 255, 255, 0.2);
  position: relative;
  backdrop-filter: blur(10px);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logo-icon {
  width: 24px;
  height: 24px;
  border: 2px solid #00ffff;
  border-radius: 50%;
  position: relative;
  animation: rotate 3s linear infinite;
}

.logo-icon::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  background: #00ffff;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #00ffff;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.dashboard-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
  color: #00ffff;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.current-time {
  font-family: 'Monaco', monospace;
  font-size: 20px;
  color: #00ffff;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
  position: relative;
  padding: 8px 15px;
  border-radius: 4px;
  background: rgba(0, 255, 255, 0.1);
}

.current-time::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 1px solid rgba(0, 255, 255, 0.3);
  border-radius: 4px;
  animation: borderPulse 2s infinite;
}

@keyframes borderPulse {
  0% { border-color: rgba(0, 255, 255, 0.1); }
  50% { border-color: rgba(0, 255, 255, 0.5); }
  100% { border-color: rgba(0, 255, 255, 0.1); }
}

/* 添加顶部装饰线 */
.dashboard-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, 
    transparent,
    rgba(0, 255, 255, 0.5),
    transparent
  );
}

.dashboard-content {
  flex: 1;
  display: flex;
  padding: 20px;
  gap: 20px;
  overflow: hidden;
}

.left-panel {
  width: 30%;
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-y: auto;
  position: relative;
  background: rgba(0, 21, 41, 0.8);
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 255, 255, 0.1);
  padding: 16px;
}

.left-panel::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg,
    rgba(0, 255, 255, 0.2),
    rgba(0, 255, 255, 0),
    rgba(0, 255, 255, 0.2)
  );
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 4s linear infinite;
  pointer-events: none;
}

.center-panel {
  width: 45%;
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: relative;
  background: rgba(0, 21, 41, 0.8);
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 255, 255, 0.1);
  overflow: hidden;
}

.center-panel::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg,
    rgba(0, 255, 255, 0.2),
    rgba(0, 255, 255, 0),
    rgba(0, 255, 255, 0.2)
  );
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 4s linear infinite;
  pointer-events: none;
}

.right-panel {
  width: 25%;
  background: rgba(0, 21, 41, 0.8);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 255, 255, 0.1);
}

.right-panel::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg,
    rgba(0, 255, 255, 0.2),
    rgba(0, 255, 255, 0),
    rgba(0, 255, 255, 0.2)
  );
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 4s linear infinite;
  pointer-events: none;
}

@keyframes borderGlow {
  0% {
    filter: hue-rotate(0deg);
  }
  100% {
    filter: hue-rotate(360deg);
  }
}

.video-section {
  flex: 1;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  overflow: hidden;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 16px;
}

.device-list {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 20px;
}

:deep(.el-button) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
}

:deep(.el-button:hover) {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
}

:deep(.el-table) {
  background: transparent !important;
}

:deep(.el-table::before) {
  display: none;
}

:deep(.el-table tr) {
  background: transparent !important;
}

:deep(.el-table td, .el-table th) {
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.weather-display {
  margin-top: auto;
  padding-top: 10px;
  text-align: center;
}

.weather-title {
  font-size: 16px;
  color: #00ffff;
  margin-bottom: 4px;
  font-weight: 500;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.weather-location {
  font-size: 12px;
  color: rgba(0, 255, 255, 0.8);
  margin-bottom: 2px;
}

.weather-time {
  font-size: 11px;
  color: rgba(0, 255, 255, 0.6);
  letter-spacing: 1px;
  margin-bottom: 10px;
}

.weather-gauge {
  height: 140px;
  width: 100%;
  margin: 5px 0;
}

.weather-info {
  text-align: center;
  margin-top: 10px;
}

.weather-temp-text {
  font-size: 15px;
  color: rgba(0, 255, 255, 0.9);
  margin-bottom: 8px;
  letter-spacing: 1px;
}

.weather-current-temp {
  font-size: 28px;
  color: #00ffff;
  font-weight: bold;
  text-shadow: 0 0 15px rgba(0, 255, 255, 0.4);
  font-family: 'Monaco', monospace;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logout-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 77, 79, 0.1);
  border: 1px solid #ff4d4f;
  color: #ff4d4f;
  transition: all 0.3s;
}

.logout-btn:hover {
  background: rgba(255, 77, 79, 0.2);
  box-shadow: 0 0 10px rgba(255, 77, 79, 0.3);
}

.logout-btn:active {
  transform: scale(0.98);
}

.logout-btn .el-icon {
  font-size: 14px;
}
</style> 